mod model;
mod api;
mod biz;
mod util;
mod request;

use dotenv::dotenv;
use rbatis::RBatis;
use poem::{listener::TcpListener, Route};
use poem_openapi::{ OpenApiService};

#[tokio::main]
async fn main() -> Result<(), std::io::Error>{
    // 创建db
    dotenv().ok(); // 加载 .env 文件中的环境变量
    let database_url = std::env::var("DATABASE_URL").expect("DATABASE_URL must be set");

    let rb = RBatis::new();
    rb.link(rbdc_pg::driver::PgDriver {}, &database_url).await.expect("connect db failed");

    let api = api::TaskListAPI { rb };
    let api_service =
        OpenApiService::new(api, "task-list", "1.0").server("http://localhost:3000/api");
    let ui = api_service.swagger_ui();
    let app = Route::new().nest("/task", api_service).nest("/docs", ui);

    poem::Server::new(TcpListener::bind("0.0.0.0:3000"))
        .run(app)
        .await
}
